package com.fingard.dsp.bank.directbank.bocom01;

import com.bocom.jump.bp.util.Base64;

import com.fingard.dsp.bank.directbank.bankInterface.IRetailMsg;
import com.fingard.dsp.bank.directbank.fngrd01.util.StringHelper;
import com.fingard.dsp.bank.reqrespobj.retailMsg.ReqRetailMsgQry;
import com.fingard.dsp.bank.reqrespobj.retailMsg.ReqRetailMsgQryDetail;
import com.fingard.dsp.bank.reqrespobj.retailMsg.RetAtsRetailMsg;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 零售信息
 * 
 * @author xtt
 *
 */
public class BOCOM01RetailMsg extends BOCOM01Base implements IRetailMsg {

	@Override
	public RetAtsRetailMsg getRetailMsg(ReqRetailMsgQry p_orderMsg) throws Exception {
		RetAtsRetailMsg retAts = new RetAtsRetailMsg(p_orderMsg.reqHeader);
		retAts.writeStart();
		String tmpRespCode = "";
		String tmpRespInfo = "";
		try {
			int pageNum = 40;
			int sendNum = 0;
			int i = 0;
			if(StringHelper.isNullOrEmpty(p_orderMsg.Z_BISHU)) {
				retAts.writeRespCode("-1");
				retAts.writeRespInfo("总笔数未填写，不允许发送交易");
				retAts.writeEnd();
				return retAts;
			}
			int total = Integer.valueOf(p_orderMsg.Z_BISHU);
			//每次推送40条
			int pageSize = total/pageNum;
			while(i < pageSize + 1) {
			BOCOM01Header bankHeader = new BOCOM01Header();
			bankHeader.tr_code = "rsvCustSellData";
			bankHeader.sign = "1.00";
			req_no = getQryTransID();
			XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
			// 写入通用的头部节点
			writeCommonHeader_3(xmlWriter, bankHeader);
			// 写入body
			xmlWriter.writeStartElement("body");
			xmlWriter.writeElementString("CUST_ID_NO",StringHelper.isNullOrEmpty(p_orderMsg.DealerCode)?getBankConfig().getValueAsString("svcustidno"):"");// 经销商证件号码
			xmlWriter.writeElementString("ID_TYPE",StringHelper.isNullOrEmpty(p_orderMsg.DealerCode)?getBankConfig().getValueAsString("idtype"):"");// 经销商证件号码
			
			xmlWriter.writeElementString("ERP_CUST_ID",p_orderMsg.DealerCode);
			xmlWriter.writeElementString("SV_CUST_ID_NO", getBankConfig().getValueAsString("svcustidno"));// 主机厂证件号码
			xmlWriter.writeElementString("SV_ID_TYPE", getBankConfig().getValueAsString("svidtype"));// 主机厂证件类型
			xmlWriter.writeElementString("TOTAL_SELL_INFO", p_orderMsg.TotalSellInfo);// 经销商零售数据汇总
			xmlWriter.writeElementString("TOTAL_STOCK_INFO", p_orderMsg.TotalStockInfo);// 经销商库存数据汇总
			xmlWriter.writeStartElement("CERTIFICATION_LIST");
			while (sendNum < pageNum && p_orderMsg.readRecord()) {
				sendNum ++;
				ReqRetailMsgQryDetail tmpDetail = p_orderMsg.ReqDetail;
				XElement tmpXElement = new XElement("CERTIFICATION", "");	
				tmpXElement.addChild(new XElement("FRAME_NUM", tmpDetail.VIN));
				tmpXElement.addChild(new XElement("CERTIFICATION_NO", tmpDetail.CertificationNo));
				tmpXElement.addChild(new XElement("CERTIFICATION_STATUS", "2"));//已实销
				tmpXElement.addChild(new XElement("SELL_PRICE", "200000.00"));
				tmpXElement.addChild(new XElement("SELL_DATE", tmpDetail.RetailTime));
				xmlWriter.writeXElement(tmpXElement);
			}
			xmlWriter.writeEndElement();
			xmlWriter.writeEndDocument();
			String reqXml = Base64.encode(xmlWriter.toXmlString().getBytes("UTF-8"));
			XmlTextWriter xmlWriters = new XmlTextWriter();// 发银行报文的XML对象
			tr_code = "FHTS01";
			writeCommonHeader(xmlWriters);
			xmlWriters.writeStartElement("body");
			xmlWriters.writeElementClosed("rem");
			xmlWriters.writeElementString("systemId", "SCF");
			xmlWriters.writeElementString("data", reqXml);
			xmlWriters.writeEndDocument();
			String[] tmpStrRet = sendToBank(xmlWriters.toXmlString());// 发送银行
			if (tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				// 解析银行返回
				// base64解码
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					String ans_code = tmpBkReader.getFirstText("/ap/head/ans_code");
					String ans_info = tmpBkReader.getFirstText("/ap/head/ans_info");
					if ("0".equals(ans_code)) {
						String particular_code = tmpBkReader.getFirstText("/ap/head/particular_code");
						String particular_info = tmpBkReader.getFirstText("/ap/head/particular_info");
						if ("0000".equals(particular_code)) {
							String body = new String(Base64.decode(tmpBkReader.getFirstText("/ap/body/RspMsgContent")),"UTF-8");
							WriteBankLogStep3(body);
							StringBuilder bodyMsg = new StringBuilder();
							// 解析银行返回
							XmlTextReader bodyBkReader = loadBankRespXmlReader(body, bodyMsg);
							if (bodyBkReader != null) {
								String tmpRetCode = bodyBkReader.getFirstText("/mes/head/RSP_STATUS");
								tmpRespInfo = bodyBkReader.getFirstText("/Mes/head/RSP_MSG");
								if (tmpRetCode.endsWith("N")) {
									while (bodyBkReader.read("/mes/body/CERTIFICATION_LIST/CERTIFICATION")) {
										String CERTIFICATION_NO = bodyBkReader.readedXElement
												.getSingleTextNotNull("CERTIFICATION_NO");
										tmpRespInfo = bodyBkReader.readedXElement
												.getSingleTextNotNull("CERTIFICATION_STATUS");
										tmpRespCode = "0";
										tmpRespInfo = CERTIFICATION_NO + " : " + tmpRespInfo;
										tmpSbMsg.append("合格证号：" + tmpRespInfo + ",");
									}
									sendNum = 0;
									i++;
								} else {
									tmpRespCode = "-1";
									tmpRespInfo = tmpRetCode;
									break;
								}					
							} else {
								tmpRespCode = "-1";
								tmpRespInfo = tmpSbMsg.toString();
								break;
							}
						} else {
							tmpRespCode = "-1";
							tmpRespInfo = particular_info;
							break;
						}
					} else {
						retAts.writeRespCode("-1");
						retAts.writeRespInfo(ans_info);
						break;
					}
				} else {
					tmpRespCode = "-1";
					tmpRespInfo = tmpSbMsg.toString();
					break;
				}
			} else {
				tmpRespCode = "-1";
				tmpRespInfo = tmpStrRet[1];
				break;
			}
		  }
		} catch (Exception e) {
			tmpRespCode = "-1";
			tmpRespInfo = e.getMessage();
			e.printStackTrace();
		}
		retAts.writeRespCode(tmpRespCode);
		retAts.writeRespInfo(tmpRespInfo);
		retAts.writeEnd();
		return retAts;
	}

}
